package com.ubao.network;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ubao.bean.Category;
import com.ubao.bean.Commodity;
import com.ubao.bean.ListCategory;
import com.ubao.bean.LoginInfo;
import com.ubao.bean.MyProductList;
import com.ubao.bean.Status;
import com.ubao.util.log.Logger;

public class JsonParser {
	private static final String TAG = "json";
	private static void printJsonStr(String str){
		Logger.d(TAG, str);
	}
	public static String getMessage(String jsonStr){
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		String message = "";
		try {
			JSONObject obj = new JSONObject(jsonStr);
			if(!obj.getString("Status").equals("200")){
				message = obj.getString("Message");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return message;
	}
	
	public static Status getStatus(String jsonStr){
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		Status status = new Status();
		try {
			JSONObject obj = new JSONObject(jsonStr);
			status.setStatus(obj.getString("Status"));
			status.setMessage(obj.getString("Message"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
	
	public static LoginInfo getLoginInfo(String jsonStr){
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		LoginInfo logininfo = new LoginInfo();
		try {
			JSONObject obj = new JSONObject(jsonStr);
			logininfo.setStatus(obj.getString("Status"));
			logininfo.setMessage(obj.getString("Message"));
			logininfo.setEmail(obj.getString("Email"));
			logininfo.setUserid(obj.getString("UserID"));
			logininfo.setUsername(obj.getString("UserName"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return logininfo;
	}
	
	public static List<Category> getCategoryList(String jsonStr){
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		List<Category> list = new ArrayList<Category>();
		try {
			JSONObject obj = new JSONObject(jsonStr);
			JSONArray array = obj.getJSONArray("Records");
			for(int i=0;i<array.length();i++){
				Category c = new Category();
				c.setCreateTime(array.getJSONObject(i).getString("CreateTime"));
				c.setTypeID(array.getJSONObject(i).getString("TypeID"));
				c.setTypeName(array.getJSONObject(i).getString("TypeName"));
				list.add(c);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
	public static ListCategory getListCategory(String jsonStr) {
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		ListCategory listc = new ListCategory();
		List<Category> list = new ArrayList<Category>();
		try {
			JSONObject obj = new JSONObject(jsonStr);
			listc.setMessage(obj.getString("Message"));
			listc.setRecordCount(obj.getString("RecordCount"));
			listc.setStatus(obj.getString("Status"));
			JSONArray array = obj.getJSONArray("Records");
			for(int i=0;i<array.length();i++){
				Category c = new Category();
				c.setCreateTime(array.getJSONObject(i).getString("CreateTime"));
				c.setTypeID(array.getJSONObject(i).getString("TypeID"));
				c.setTypeName(array.getJSONObject(i).getString("TypeName"));
				list.add(c);
			}
			listc.setListCategory(list);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return listc;
	}
	public static MyProductList getCommoditylist(String jsonStr) {
		jsonStr = URLDecoder.decode(jsonStr);
		printJsonStr(jsonStr);
		MyProductList products = new MyProductList();
		List<Commodity> list = new ArrayList<Commodity>();
		try {
			JSONObject obj = new JSONObject(jsonStr);
			products.setMessage(obj.getString("Message"));
			products.setRecordCount(obj.getString("RecordCount"));
			products.setStatus(obj.getString("Status"));
			JSONArray array = obj.getJSONArray("Records");
			for(int i=0;i<array.length();i++){
				Commodity c = new Commodity();
				c.setId(array.getJSONObject(i).getString("id"));
				c.setCommodityName(array.getJSONObject(i).getString("CommodityName"));
				c.setCommodityDescription(array.getJSONObject(i).getString("CommodityDescription"));
				c.setReleaseDate(array.getJSONObject(i).getString("ReleaseDate"));
				c.setCommodityType(array.getJSONObject(i).getString("CommodityType"));
				c.setCommodityTypeName(array.getJSONObject(i).getString("CommodityTypeName"));
				c.setIsExchange(array.getJSONObject(i).getString("IsExchange"));
				c.setIsShare(array.getJSONObject(i).getString("IsShare"));
				c.setCreateTime(array.getJSONObject(i).getString("CreateTime"));
				c.setScore(array.getJSONObject(i).getString("Score"));
				c.setLongitude(array.getJSONObject(i).getString("Longitude"));
				c.setLatitude(array.getJSONObject(i).getString("Latitude"));
				c.setCommentNo(array.getJSONObject(i).getString("CommentNo"));
				c.setUserID(array.getJSONObject(i).getString("UserID"));
				c.setCommodityID(array.getJSONObject(i).getString("CommodityID"));
				c.setCountry(array.getJSONObject(i).getString("Country"));
				c.setCity(array.getJSONObject(i).getString("City"));
				c.setLocality(array.getJSONObject(i).getString("Locality"));
				c.setSubLocality(array.getJSONObject(i).getString("SubLocality"));
				c.setStarNo(array.getJSONObject(i).getString("StarNo"));
				c.setStarNoOfOne(array.getJSONObject(i).getString("StarNoOfOne"));
				c.setImagePath(array.getJSONObject(i).getString("imagePath"));
				c.setViewNo(array.getJSONObject(i).getString("ViewNo"));
				c.setShelfTime(array.getJSONObject(i).getString("ShelfTime"));
				c.setFollowingNo(array.getJSONObject(i).getString("FollowingNo"));
				list.add(c);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally{
			products.setRecords(list);
		}
		return products;
	}
	
	
}
